<script setup name="name">
import * as echarts from 'echarts'
import { markRaw, nextTick } from 'vue'
const props = defineProps({
  xAxisData: {
    type: Array,
    default: () => []
  },
  yAxisData: {
    type: Array,
    default: () => []
  },
  width: {
    type: String,
    default: '100%'
  },
  height: {
    type: String,
    default: '400px'
  },
  borderRadius: {
    type: String || Number,
    default: 10
  },
  tooltipShow: {
    type: Boolean,
    default: true
  },
  legendShow: {
    type: Boolean,
    default: true
  },
  fontSize: {
    type: Number,
    default: 10
  },
  xh: {
    type: Boolean,
    default: true
  }
})
// 线边仓实例
const lineEdgeRef = ref(null)

const myChart = ref()
const option = {
  legend: {
    // 图例
    show: true,
    orient: 'horizontal',
    right: '1%',
    top: '-1%',
    textStyle: {
      color: '#fff'
    }
  },
  tooltip: {
    //图例提示框，默认不显示
    trigger: 'axis',

    axisPointer: {
      //---指示样式
      type: 'shadow'
    }
  },
  // echarts固定高度
  grid: {
    left: '1%',
    right: '1%',
    bottom: '3%',
    top: '10%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: [],
    axisTick: {
      alignWithLabel: true
    }
  },
  yAxis: {
    type: 'value'
    // axisLabel: {
    //   interval: 0,
    //   rotate: 0,
    //   color: '#21C1FF',
    //   fontSize: '16px'
    // }
  },
  series: [
    {
      name: '出库',
      type: 'bar',
      itemStyle: {
        normal: {
          color: '#679bf7'
        }
      },
      barWidth: '30%',
      data: [],
      label: {
        // 运行时间层叠的数量
        show: true
      },
      emphasis: {
        focus: 'series'
      }
    },
    {
      name: '入库',
      type: 'bar',
      barWidth: '30%',
      itemStyle: {
        normal: {
          color: '#5fb3bb'
        }
      },
      data: [],
      label: {
        show: true
      },
      emphasis: {
        focus: 'series'
      }
    }
  ]
}
const tiemr = ref(null)

const initData = () => {
  option.xAxis.data = props.xAxisData
  // props.yAxisData.forEach(item => {
  // })
  clearInterval(tiemr.value)
  option.series[0].data = props.yAxisData[0]
  option.series[1].data = props.yAxisData[1]
  // 初始化echart
  const chartRefWrap = unref(lineEdgeRef)
  if (chartRefWrap) {
    myChart.value = markRaw(echarts.init(chartRefWrap))

    myChart.value.setOption(option)
    window.addEventListener('resize', chartsResize)
    //自动切换顺序
    var cityIndex = 0
    if (!props.xh) return
    tiemr.value = setInterval(function () {
      if (cityIndex < props.xAxisData.length) {
        init(cityIndex)
        cityIndex++
      } else {
        cityIndex = 0
        init(cityIndex)
        cityIndex++
      }
    }, 1000)
  }
}

// 动态
const init = cityIndex => {
  myChart.value.dispatchAction({ type: 'downplay', seriesIndex: 0 })
  myChart.value.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: cityIndex })
  myChart.value.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: cityIndex })
}

const chartsResize = () => {
  myChart.value && myChart.value.resize()
}

nextTick(() => {
  initData()
})

watch(
  () => props.yAxisData,
  val => {
    if (val.length > 0) {
      clearInterval(tiemr.value)
      nextTick(() => {
        initData()
      })
    }
  },
  {
    deep: true,
    immediate: true
  }
)
</script>

<template>
  <div ref="lineEdgeRef" :style="{ width: props.width, height: props.height }"></div>
</template>

<style lang="scss" scoped>
.ss {
  background-color: red;
}
</style>
